<!--
 * @Author: liujinjun681 liujinjun681@qq.com
 * @Date: 2022-09-08 16:04:23
 * @LastEditors: liujinjun681 liujinjun681@qq.com
 * @LastEditTime: 2022-11-18 15:37:51
 * @FilePath: \hd_ims_web\src\views\output\outputData\qrCode\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container" @click="focusInp">
    <input
      id="inpRrqId"
      type="text"
      v-model="form.rrqId"
      autofocus="autofocus"
      @blur="(e) => e.target.focus()"
      @keyup.enter="scanQr"
      style="height: 0px; width: 0px; border: none; opacity: 0"
    />

    <el-row :gutter="20">
      <el-col :span="8">
        <el-card>
          <div slot="header">
            <span>扫码出库操作台</span>
            <el-button
              style="float: right; padding: 3px 0"
              type="text"
              @click="clearALL"
              >重新开始扫码</el-button
            >
          </div>

          <div style="font-size: 18px; color: magenta">
            当前已扫码识别{{ num }}次，成功{{ numSuccess }}次!
          </div>
          <ol
            class="ollist"
            element-loading-text="扫码识别中..."
            element-loading-spinner="el-icon-loading"
            v-loading="loading || !this.num"
          >
            <li>开单日期：{{ $moment(itemCur.rrqRq).format("YYYY-MM-DD") }}</li>
            <li>班次：{{ itemCur.rrqBc }}</li>
            <li>品名规格：{{ itemCur.itmPmgg }}</li>
            <li>客户代码：{{ itemCur.itmCustwldm }}</li>
            <li>数量：{{ itemCur.rrqNumber }}</li>
            <li>箱号：{{ itemCur.rrqXs }}</li>
          </ol>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card>
          <div slot="header">
            <span>扫码出库信息</span>
          </div>
          <div class="scrollbarQr" id="printContent">
            <el-table :data="listQr" stripe>
              <el-table-column
                label="品名规格"
                prop="itmPmgg"
                min-width="200px"
              />
              <el-table-column label="物料代码" prop="rrqWldm" width="120px" />
              <el-table-column
                label="客户代码"
                prop="itmCustwldm"
                width="100px"
              />
              <el-table-column label="生产日期" prop="rrqRq" width="100px">
                <template #default="scope">
                  {{ $moment(scope.row.rrqRq).format("YYYY-MM-DD") }}
                </template>
              </el-table-column>
              <el-table-column label="班次" prop="rrqBc" width="60px" />
              <el-table-column label="箱号" prop="rrqXs" width="60px" />
              <el-table-column label="入库时间" prop="rrqRkTime" width="120px">
                <template #default="scope">
                  {{
                    $moment(scope.row.rrqRkTime).format("YYYY-MM-DD HH:mm:ss")
                  }}
                </template>
              </el-table-column>
              <el-table-column label="出库时间" prop="rrqCkTime" width="120px">
                <template #default="scope">
                  {{
                    $moment(scope.row.rrqCkTime).format("YYYY-MM-DD HH:mm:ss")
                  }}
                </template>
              </el-table-column>
              <el-table-column label="入库仓库" prop="itmCkdm" width="100px">
                <template #default="scope">
                  <dict-tag
                    :options="warehouse_list"
                    :value="scope.row.itmCkdm"
                  />
                </template>
              </el-table-column>
              <el-table-column label="数量" prop="rrqNumber" width="80px" />
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <audio
      style="display: none"
      ref="audio"
      id="scan_success"
      src="@/assets/audio/scan_success.wav"
    ></audio>
    <audio
      style="display: none"
      ref="audiod"
      id="scan_fail"
      src="@/assets/audio/scan_fail.wav"
    ></audio>
    <audio
      style="display: none"
      ref="audiod"
      id="stocks_low"
      src="@/assets/audio/stocks_low.wav"
    ></audio>
    <audio
      style="display: none"
      ref="audiod"
      id="operation_fail"
      src="@/assets/audio/operation_fail.wav"
    ></audio>
  </div>
</template>

<script>
import { getAll } from "@/api/repertory/Storage";
import {
  generateQr,
  warehousingQr,
  outOfStockQr,
  outOfByCustomer,
} from "@/api/repertory/putstorage";
import {
  automaticStorage,
  PDACusWldmList,
  getItmMstrByPDACustwldm,
  getStatisticsWarehousing,
} from "@/api/repertory/PDA";
import { speak } from "@/utils/index";
export default {
  name: "OutWareQr",
  data() {
    return {
      form: {
        rrqBc: null,
        rrqRq: null,
        rrqWldm: null,
        rrqNumber: 0,
        count: "",
        rrqId: "",
      },
      date: "",
      BcDm: "白班",
      customerCode: "",
      itemCur: {},
      pm: "",
      listId: [],
      listQr: [],
      loading: false,
      num: 0,
      numSuccess: 0,
      //提供的所有客户代码
      provider_list_data: [],
      //符合条件的客户代码
      provider_list: [],
      //入库仓库
      warehouse_list: [],
      //入库类型
      type_list: [],
      //入库客户
      kh_list: [],
    };
  },
  created() {
    //请求所有客户代码
    PDACusWldmList().then((response) => {
      this.provider_list_data = response.data;
    });

    //所有仓库
    getAll().then((response) => {
      this.warehouse_list = response.data.map((o) => {
        o.dictLabel = o.storageName;
        o.dictValue = "" + o.storageId;
        return o;
      });
    });
  },
  mounted() {
    this.scan_success = document.getElementById("scan_success");
    this.scan_fail = document.getElementById("scan_fail");
    this.stocks_low = document.getElementById("stocks_low");
    this.operation_fail = document.getElementById("operation_fail");
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      setTimeout(vm.focusInp, 300);
      setTimeout(vm.focusInp, 600);
      setTimeout(vm.focusInp, 900);
      // 通过 `vm` 访问组件实例
    });
  },
  methods: {
    msg(msg, type) {
      this.$message({ showClose: true, message: msg, type: "success" });
    },
    scanQr(id) {
      // 支持识别外部二维码 LB]A0004542]240705]12131000095976]40]240705389317]]
      let tmp = this.form.rrqId.split("]");
      let req;
      if (tmp.length > 6) {
        const cus = tmp[3];
        const num = tmp[4];
        console.log(cus, num);
        req = outOfByCustomer(cus, num);
        this.form.rrqId = "";
      } else {
        if (
          !this.form.rrqId ||
          this.form.rrqId.indexOf("2022-") == -1 ||
          this.loading
        ) {
          this.form.rrqId = "";
          return;
        }
        req = outOfStockQr(this.form.rrqId);
        //this.listId.push(tmpId)
      }
      this.num++;
      this.loading = true;
      req
        .then((res) => {
          this.itemCur = res.data;
          this.listQr.unshift(res.data);
          this.listQr = this.listQr.slice(0, 30);
          this.numSuccess++;
          setTimeout(() => {
            this.loading = false;
          }, 500);
          document.getElementById("inpRrqId").focus();
          this.scan_success.play();
        })
        .catch((e) => {
          setTimeout(() => {
            this.loading = false;
          }, 500);
          console.log(e.message, e);
          if (e.message.indexOf("库存数量不足") > 0) {
            this.stocks_low.play();
            return;
          } else {
            speak(e.message);
          }
          this.$notify.error({
            title: "错误",
            message: e.message,
            duration: 10000,
          });
        });
      this.form.rrqId = "";
    },
    focusInp() {
      document.getElementById("inpRrqId").focus();
    },
    clearALL() {
      document.getElementById("inpRrqId").focus();
      this.loading = false;
      this.listQr = [];
      this.listId = [];
      this.num = 0;
      this.numSuccess = 0;
      this.itemCur = {};
    },
    getAllCustomer(queryString, cb) {
      if (this.customerCode == null || this.customerCode == "") {
        return;
      }
      let temp = [];
      this.clearALL();

      temp = this.provider_list_data
        .filter((i) => {
          return i.itmCustwldm.substr(-6).indexOf(this.customerCode) != -1;
        })
        .slice(0, 30);
      cb(temp);
    },
    FillAttribute() {
      if (this.customerCode == null || this.customerCode == "") {
        return;
      }

      getItmMstrByPDACustwldm({ itmCustwldm: this.customerCode }).then(
        (res) => {
          if (res.code == "200") {
            this.form.rrqWldm = res.data.itmWldm;
            this.pmgg = res.data.itmPmgg;
            this.rkkh = res.data.itmKhdm;
            this.rkck = res.data.itmCkdm;
            this.pm = res.data.itmWlpm;
            this.$Message.success("成功");
          } else {
            alert(res.msg);
          }
        }
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.ollist {
  list-style-type: none;
  margin: 15px 0px;
  width: 200px;
}
.divqr {
  display: flex;
  page-break-inside: avoid;
}
.scrollbarQr {
}
</style>
